Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds bsd support #1

Merged
merged 1 commit into from Jan 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,6 +1,6 @@
[package]
name = "process_path"
version = "0.1.1"
version = "0.1.2"
authors = ["Wesley Wiser <wwiser@gmail.com>"]
license = "MIT/Apache-2.0"
repository = "https://github.com/wesleywiser/process_path"
@@ -31,11 +31,14 @@ This program prints its path to stdout:

## Supported Platforms

Platform | Underlying API
-------- | --------------
Linux | `readlink(/proc/self/exe)`
macOS | `_NSGetExecutablePath()`
Windows | `GetModuleFileName()`
Platform | Underlying API
------------ | ---------------------------------------------
Linux | `readlink(/proc/self/exe)`
FreeBSD | `sysctl(3)` or `readlink(/proc/curproc/file)`
NetBSD | `readlink(/proc/curproc/exe)`
DragonflyBSD | `readlink(/proc/curproc/file)`
macOS | `_NSGetExecutablePath()`
Windows | `GetModuleFileName()`

## License

@@ -0,0 +1,51 @@
use std::fs::read_link;
use std::path::PathBuf;
use libc::*;
use std::ptr;

// http://stackoverflow.com/q/799679
// http://stackoverflow.com/a/1024937

#[cfg(target_os="netbsd")]
pub fn get_executable_path() -> Option<PathBuf> {
read_link("/proc/curproc/exe").ok()
}

#[cfg(target_os="dragonfly")]
pub fn get_executable_path() -> Option<PathBuf> {
read_link("/proc/curproc/file").ok()
}

#[cfg(target_os="freebsd")]
pub fn get_executable_path() -> Option<PathBuf> {
let mib = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1];
let mut buf: Vec<u8> = Vec::with_capacity(1024);
let mut cb = 1024 as size_t;
let result = unsafe {
sysctl(mib.as_ptr(),
Copy link
Owner

@wesleywiser wesleywiser Jan 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern at this point, is that on NetBSD & DragonflyBSD, we're calling this sysctl that only works on FreeBSD. It seems that could have unintended consequences. I suggest we use the cfg!() macro to do platform detection. For example:

if cfg!(target_os = "freebsd") {
//Freebsd sysctl call
} else {
//fallback to readlink on /proc/curproc/{exe,file}
}

Copy link
Contributor Author

@conradkleinespel conradkleinespel Jan 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll do that once I have my different BSD environments running. FreeBSD will probably need an entirely different function since it uses either sysctl or a /proc file. Either way, I'll update the PR once I've pushed these modifications. Thanks for your feedback !

4,
buf.as_mut_ptr() as *mut c_void,
&mut cb as *mut size_t,
ptr::null(),
0)
};

// FreeBSD without procfs
if result == 0 {
// Convert the string allocated on the stack to a Rust string.
let len = unsafe { strlen(buf.as_ptr() as *const i8) };
unsafe { buf.set_len(len) };
match String::from_utf8(buf).ok() {
Some(path) => {
let mut pb = PathBuf::new();
pb.push(path);
Some(pb)
}
None => None,
}
} else {
// FreeBSD with procfs
read_link("/proc/curproc/file").ok()
}

}
@@ -25,14 +25,19 @@ mod macos;
#[cfg(target_os = "macos")]
use macos as os;

#[cfg(any(target_os="freebsd", target_os="dragonfly", target_os="netbsd"))]
mod bsd;
#[cfg(any(target_os="freebsd", target_os="dragonfly", target_os="netbsd"))]
use bsd as os;

#[cfg(windows)]
mod windows;
#[cfg(windows)]
use windows as os;


/// Gets the path of the currently running process. If the path cannot be determined,
/// `None` is returned.
/// `None` is returned.
pub fn get_executable_path() -> Option<PathBuf> {
os::get_executable_path()
}